package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import transfer.CreateLeagueRoomData;
import util.Utils;
import widget.MyButtonWidget;
import callback.BackToLobbyCallback;

import client.Client;

public class CreateLeagueRoomForm extends ChessOnlineView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4479890064134027135L;

	public static final String[] SCORE_BET = {"10", "20", "30"};
	
	private JTextField mRoomNameTB;
	private JComboBox mBetScoreCB;
	private MyButtonWidget mBackBtn;
	//private ChessOnlineRoom room = null;
	public CreateLeagueRoomForm(Client mainScreen) {
		super(mainScreen);
		
		// Create form with 2 text field and Login button
		this.setSize(500, 250);
		
		GridBagLayout gridbag = new GridBagLayout();
		setLayout(gridbag);
		GridBagConstraints c = new GridBagConstraints();
		
		JLabel roomNameLBL = new JLabel("League Room Name: ");
		roomNameLBL.setForeground(Color.cyan);
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		add(roomNameLBL, c);
		mRoomNameTB = new JTextField(30);
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		add(mRoomNameTB, c);

		JLabel scoreLBL = new JLabel("Choose the score: ");
		scoreLBL.setForeground(Color.cyan);
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 2;
		add(scoreLBL, c);
		mBetScoreCB = new JComboBox(SCORE_BET);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.8;
		c.gridx = 1;
		c.gridy = 2;
		add(mBetScoreCB, c);
		
		JButton createCmd = new JButton("Create");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 3;
		add(createCmd, c);
		
		createCmd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String roomName = mRoomNameTB.getText();
				int score = Integer.valueOf((String)mBetScoreCB.getSelectedItem());
				System.out.println("Current League Id: " + Utils.getLeagueId());
				mMainScreen.getCw().sendObject(new CreateLeagueRoomData("Create League Room",roomName,mMainScreen.getAcc(),score, Utils.getLeagueId()));
			}
		});
		
		mBackBtn = new MyButtonWidget(650, 450, "Resources/Images/back-icon.png", 
				"Resources/Images/back-icon.png", "", new BackToLobbyCallback(mMainScreen,null));
		this.add(mBackBtn);
		this.addMouseListener(mMyListener);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		mBackBtn.paint(g);
	}
	
	private MouseListener mMyListener = new MouseAdapter() {
		public void mouseClicked(java.awt.event.MouseEvent e) {
			mBackBtn.onClick(e);
		};
	};


}
